<template>
	<cl-crud ref="crud" @load="onLoad" :on-refresh="onRefresh">
		<cl-pagefull @resize="onResize">
			<template #head>
				<cl-title-module
					title="部门报表"
					sub-title="以部门人员为单位进行汇总（当前登陆账号所管理的部门）"
					:columns="initColumns"
					:print="printParams"
					v-model="table.columns"
					@export-file="exportExcel"
				/>
				<el-row type="flex">
					<cl-filter-group v-model="search">
						
						<cl-filter label="事业部">
							<cl-select
								v-model="search.biz_id"
								placeholder="请选择"
								filterable
								@change="changeBiz"
							>
								<el-option
									v-for="item in business"
									:key="item.biz_id"
									:label="item.biz_name"
									:value="item.biz_id"
								/>
							</cl-select>
						</cl-filter>
						<cl-filter label="所属门店">
							<cl-shop-select
								v-model="search.dept_id"
								placeholder="请选择"
								filterable
								style="width: 150px"
								clearable
								@change="changeDept"
								show-all
								:biz_id="search.biz_id"
							/>
						</cl-filter>
						<cl-filter label="区分门店">
							<cl-select
								v-model="search.type"
								style="width: 150px"
							>
								<el-option label="区分" :value="1" />
								<el-option label="不区分" :value="2" />
							</cl-select>
						</cl-filter>
						<cl-filter label="门店老师">
							<teacherSelect ref="teacherSelect" v-model="search.teachers" />
						</cl-filter>
						<cl-filter label="时间">
							<cl-date-picker
								v-model="search.date"
								type="daterange"
								range-separator="至"
								start-placeholder="开始日期"
								end-placeholder="结束日期"
								value-format="yyyy-MM-dd"
								style="width: 230px"
								:clearable="false"
							/>
						</cl-filter>
						
					</cl-filter-group>
				</el-row>
			</template>
		
			<!-- 数据表格 -->
            <cl-table v-bind="table" id="deptReport" ref="table">
			</cl-table>
		</cl-pagefull>
	</cl-crud>
</template>
<script>
import { exportExcel } from "@/utils/excel";
import { cloneDeep } from "cl-admin/utils";
import dayjs from "dayjs";
import { last } from "cl-admin/utils";
import { delEmptyChildren } from "@/utils";
import teacherSelect from "@/components/teacher-select";

export default {
	components: {
		teacherSelect
	},
	data() {
		return {
			printParams: {
				id: "deptReport",
				popTitle: "部门报表"
			},
			// 表格配置
			table: {
				"context-menu": ["refresh"],
				columns: [],
				props: {
					maxHeight: 500,
					showSummary: true,
					summaryMethod: () => {
                        return this.summariesArr;
                    },
				}
			},
			initColumns: [],
            summariesArr: [],
			search: {
				date: [
					dayjs()
						.startOf("day")
						.format("YYYY-MM-DD"),
					dayjs()
						.endOf("day")
						.format("YYYY-MM-DD")
				],
				biz_id: "",
				dept_id: "",
				type: 1,
				teachers: "",
			},
			deptList: [], //部门
		};
	},
	computed: {
		bizRefData() {
			let { biz_id } = this.search;
			return this.getBizRefData(biz_id);
		}
	},
	created() {
		this.search.biz_id = this.getCurrBiz();
		this.init();
	},
	methods: {
		onLoad({ ctx, app }) {
			ctx.service(this.$service.finance)
				.set("dict", {
					api: {
						page: "teacherreport"
					}
				})
				.done();
			app.refresh(this.search);
		},
		async onRefresh(params, { next, done, render }) {
			let { title, data } = await next(params);
			let columns = (title ?? []).map((v, i) => {
				return {
					disable: i == 0,
					fixed: i == 0,
					prop: v.key,
					label: v.name,
					minWidth: 170,
					sortable: true
				};
			});
            console.log(data, 'onRefresh')
			this.initColumns = columns;
			this.table.columns = columns
			// !this.table.columns.length && (this.table.columns = columns);
			render(data);
		},
		async init(type = 0) {
			const { biz_id } = this.search;
			this.getBizRefShop(biz_id);
			this.initData(type);
		},
		async initData(type = 0) {
			const { biz_id } = this.search;

			this.deptList = delEmptyChildren(
				(await this.$store.dispatch("common/getDept", {
					biz_id,
					post: 2,
					needAll: false,
					needCascader: true
				})) || []
			);
			this.changeDept();
		},
		//切换事业部
		changeBiz() {
			this.search.dept_id = "";
			this.init(1);
		},
		// crud刷新
		refresh() {
			this.$refs.crud.refresh({ page: 1, ...this.search });
			this.$nextTick(() => {
				this.$refs.table?.calcMaxHeight?.();
			});
		},
		onResize(h) {
			this.table.props.maxHeight = h;
		},
		// 表格合计
		getSummaries({ columns, data }) {
			let old_amount = 0,
				old_arrive = 0,
				new_amount = 0,
				new_arrive = 0;
                total_amount = 0;
                total_arrive = 0;
			let arr = ["otn_arrival_rate", "manage_shop"];
			return columns.map((column, index) => {
				if (index === 0) {
					return "合计";
				}
			});
		},
		// 切换门店
		changeDept() {
			this.search.teachers = "";
			this.getTeacherList();
			// this.refresh();
		},
		// 老师列表
		async getTeacherList() {
			const { biz_id, dept_id } = this.search;
			if (!biz_id) return;

			this.$nextTick(() => {
				this.$refs.teacherSelect.init({
					dept_id,
					biz_id,
					status: 1,
					is_teacher: 1
				});
			});
		},
		// 导出EXCEl
		exportExcel() {
			exportExcel("#deptReport");
		}
	}
};
</script>
<style lang="scss" scoped>
::v-deep {
	.el-table--scrollable-x {
		.el-table__body-wrapper {
			z-index: 2;
		}
	}
}
</style>
